//
//  eight.m
//  享动
//
//  Created by lanou on 16/4/18.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "eight.h"
#import "musicChooseCell.h"
#import "RequestManager.h"
#import "musicRunModel.h"
#import <MJRefresh.h>
@interface eight () <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray * modelArray;

@property(nonatomic,assign)NSInteger startIndex;

@property(nonatomic,strong)NSMutableArray * anotherArr;

@property(nonatomic,assign)NSInteger a;

@property(nonatomic,strong)NSMutableArray * backArray;

@end


@implementation eight

- (void)get
{
    
    NSDictionary * dic = @{@"index":@"0"};
    NSString * url = @"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=a87cc5e228db4a82bdfffedbcd600b86&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&scene=6&uid=301026";
    [RequestManager requestWithUrl:url requestType:RequestTypePost parDic:dic finish:^(NSData *data) {
        NSDictionary * allDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * resultArray  = allDic[@"page"][@"result"];
        
        
        for (NSDictionary * one in resultArray) {
            
            musicRunModel * model = [[musicRunModel alloc]init];
            
            [model setValuesForKeysWithDictionary:one];
            
            [_modelArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            
        });
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
    
}
- (void)loadMore
{
    
    _startIndex += 1;
    
    NSDictionary * dic = @{@"index":[NSString stringWithFormat:@"%ld",_startIndex]};
    
    NSString * url =@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=a87cc5e228db4a82bdfffedbcd600b86&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&scene=6&uid=301026" ;
    [RequestManager requestWithUrl:url requestType:RequestTypePost parDic:dic finish:^(NSData *data) {
        NSDictionary * allDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * resultArray  = allDic[@"page"][@"result"];
        
        for (NSDictionary * one in resultArray) {
            
            musicRunModel * model = [[musicRunModel alloc]init];
            
            [model setValuesForKeysWithDictionary:one];
            
            [_anotherArr addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_modelArray addObjectsFromArray:_anotherArr];
            [self.tableView reloadData];
            
        });
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _anotherArr = [NSMutableArray array];
    _modelArray = [NSMutableArray array];
    _backArray = [NSMutableArray array];
    [self get];
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 35;
    [self.tableView registerClass:[musicChooseCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMore];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    musicChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.numberLabel.text = [NSString stringWithFormat:@"%02ld .",indexPath.row+1];
    musicRunModel * model = _modelArray[indexPath.row];
    
    cell.nameLabel.text = model.name;
    
    
    if (model.isSelect) {
        [cell.chooseButton setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
    }
    else
    {
        [cell.chooseButton setImage:[UIImage imageNamed:@"noChoose"] forState:UIControlStateNormal];
    }
    
    
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    musicChooseCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    musicRunModel *model = _modelArray[indexPath.row];
    
    if ([_controllerStr isEqualToString:@"musci"]) {

    if (model.isSelect) {
        [_backArray removeObject: model];
        [cell.chooseButton setImage:[UIImage imageNamed:@"noChoose"] forState:UIControlStateNormal];
    }
    else
    {
        [_backArray addObject:model];
        [cell.chooseButton setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
    }
    
    model.isSelect = !model.isSelect;
    
    // block 传值
    NSInteger count = 0;
    for (musicRunModel * model in _modelArray) {
        if (model.isSelect == YES) {
            count ++;
        }
    }
    
    _a = count;
    
    if (self.BlockC != nil) {
        self.BlockC(_a,_backArray);
    }
    } else if ([_controllerStr isEqualToString:@"run"]) {
        NSMutableArray *musicArray = [NSMutableArray array];
        for (musicRunModel *model in _modelArray) {
            [musicArray addObject:model.url];
        }
        [PlayManager sharedManager].index = indexPath.row;
        [[PlayManager sharedManager]setMusicArray:musicArray];
        [self.nav popViewControllerAnimated:YES];
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
